home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12874 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: sooner.net!usenet
  2. From: Eddie Bush <edwbush@sooner.net>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: beginner question
  5. Date: Wed, 03 Apr 1996 03:08:32 -0800
  6. Organization: sooner.net news site
  7. Message-ID: <31625C30.12AA@sooner.net>
  8. References: <4jc3sr$1ggu@uvaix3e1.comp.UVic.CA> <4jdo7l$de2@sparcserver.lrz-muenchen.de> <315AFED2.7466@willows.com> <315F525A.4A1CD496@alcyone.com>
  9. NNTP-Posting-Host: p22.sooner.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Erik Max Francis wrote:
  16. > Tarang Deshpande wrote:
  17. > > So then what does the following mean:
  18. > >
  19. > > struct _FOO
  20. > > {
  21. > >         int     bar;
  22. > > } FOO;
  23. > >
  24. > > struct _FOO s1;
  25. > > FOO         s2;
  26. > >
  27. > > Why?
  28. > This is a compiler error.
  29.  
  30. I'm sorry.  I don't see an error -- just two different variables of the same type that happen to 
  31. be declared using two different (and correct) methods.
  32.  
  33. >     struct _FOO { int bar; } FOO;
  34. > declares a structure called struct _FOO and an instance of that structure FOO.
  35. > The further declaration
  36.  
  37. The initial declaration of the structure doesn't make available FOO for manipulation.  FOO is a 
  38. type.  ...isn't it?  I always understood that 'struct _FOO' is the 'tag name' (the name of the 
  39. structure', and that FOO would be a type which you have defined (by the typedef struct _FOO) to 
  40. be the structure _FOO.
  41.  
  42. >     FOO s2;
  43. > and is tantamount to saying
  44. >     int x, y;
  45. >     x y;
  46. > What you're probably _actually_ seeing is
  47. >     typedef struct _FOO { int bar; } FOO;
  48. > which defines the type FOO which is an alias for struct _FOO.
  49. > --
  50. > Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
  51. > San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
  52. > H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
  53. > Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
  54. > "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
  55.  
  56. Maybe I just do things differently.  I put all of my declarations of types into a *.h file
  57. and reference them from that by '#include "mytype.h"'.  I don't see a difference though.  You 
  58. aren't declaring any variables -- only variable types.
  59.  
  60. Also, from what I understand, you would do it like this:
  61.  
  62. typedef struct {
  63.     int num;
  64. } FOOBAR;
  65.  
  66. This would be if you intended to use the type statically.  Or, you could:
  67.  
  68. typedef struct FOO {
  69.     int num;
  70. } *BAR;
  71.  
  72. if you happened to want to allocate the variable dynamically.
  73.  
  74. Notice, that I left the 'tag name' off of the variable which I intend to use statically.  That is 
  75. because it is not needed -- you only need it to allocate memory dynamically:
  76.  
  77. BAR new_bar;
  78.  
  79. new_bar = (BAR) malloc (sizeof (struct FOO));
  80.  
  81. Maybe this helps (and doesn't confuse) someone.  I felt very confused by the string of preceding 
  82. messages -- maybe it's the time of day.  Who knows...
  83.  
  84. Later, and Good Luck!
  85.  
  86. Eddie Bush
  87.